﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinLogicUniversity.DBBroker
{
    class PurchaseOrderSend
    {
        static LogicUniversityT2Entities logic = new LogicUniversityT2Entities();
        public PurchaseOrderSend() { }
        public PurchaseOrderSend(string s)
        {
           supplierID= s;
        }
        public PurchaseOrderSend(string s,string c)
        {
            supplierID = s;
            clerkID = c;
        }
        public static string supplierID
        {get;set;}
        public static string clerkID
        { get; set; }
        public static IQueryable getPurchaseList()
        {   
            var polist = from poDetail in logic.PurchaseDetails
                         join plist in logic.PurchaseOrders on poDetail.PurchaseOrderID equals plist.PurchaseOrderID
                         join sup in logic.Suppliers on poDetail.PurchaseOrder.SupplierID equals sup.SupplierID
                         join itemDetail in logic.ItemDetails on poDetail.ItemID equals itemDetail.ItemID
                         where poDetail.PurchaseOrder.SupplierID == supplierID
                         select new
                         {
                             Item_No = itemDetail.ItemID,
                             Item_Description = itemDetail.Item.ItemDescription,
                             Quantity = poDetail.OrderedQuantity,
                             Price = itemDetail.Price,
                             Amount_Item = poDetail.OrderedQuantity * itemDetail.Price,


                         };
            return polist;
            
        }
        public static IQueryable getAllSupplier()
        {
            var sup = from supplier in logic.Suppliers select new { SID=supplier.SupplierID,SName=supplier.SupplierName};
            return sup;
        }
        public static double getTotal()
        {
            double totalAmount = 0.0, amount = 0.0;
            var polist = from poDetail in logic.PurchaseDetails
                         join plist in logic.PurchaseOrders on poDetail.PurchaseOrderID equals plist.PurchaseOrderID
                         join sup in logic.Suppliers on poDetail.PurchaseOrder.SupplierID equals sup.SupplierID
                         join itemDetail in logic.ItemDetails on poDetail.ItemID equals itemDetail.ItemID
                         where poDetail.PurchaseOrder.SupplierID == supplierID
                         select new
                         {
                             Item_No = itemDetail.ItemID,
                             Item_Description = itemDetail.Item.ItemDescription,
                             Quantity = poDetail.OrderedQuantity,
                             Price = itemDetail.Price,
                             Amount_Item = poDetail.OrderedQuantity * itemDetail.Price,

                         };
            foreach (var total in polist)
            {
                amount = Convert.ToDouble(total.Amount_Item);
                totalAmount += amount;
            }
            return totalAmount;
            
            
        }
        
        public static string getSupplierEmail()
        {   var sup = from supplier in logic.Suppliers
             where supplier.SupplierID==supplierID
              select new { supEmail=supplier.EmailAddress};
            string plswork = "";
            foreach (var id in sup)
            { plswork = id.supEmail; }
             return plswork;
        }
        public static string getSenderEmail()
        {
            var clerk = from user in logic.InventoryUsers
                        where user.EmployeeID == clerkID
                        select new { senderEmail = user.Email };
            string plswork = "";
            foreach (var id in clerk)
            {plswork = id.senderEmail;}
            return plswork;
        }
    }
}
